package com.ikkong.shine.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.model.AjaxResult;
import com.ikkong.core.toolbox.Func;
import com.ikkong.core.toolbox.Record;
import com.ikkong.shine.meta.intercept.QueueTpyeInterept;
import com.ikkong.shine.model.Patient_queue;
import com.ikkong.shine.model.QueueType;
import com.ikkong.shine.service.Patient_queueService;
import com.ikkong.shine.service.QueueTypeService;
import com.ikkong.shine.service.impl.Patient_queueServiceImpl;
import com.ikkong.shine.service.impl.QueueTypeServiceImpl;
import com.ikkong.system.controller.base.UrlPermissController;
import com.jfinal.kit.StrKit;

/**
 * Generated by Blade.
 * 2017-03-17 11:02:35
 */
public class QueueTypeController extends UrlPermissController {
	private static String CODE = "queueType";
	private static String PERFIX = "queue_type";
	private static String LIST_SOURCE = "QueueType.list";
	private static String VIEW_SOURCE="QueueType.view";
	private static String BASE_PATH = "/platform/queueType/";
	
	QueueTypeService service = new QueueTypeServiceImpl();
	Patient_queueService qservice=new Patient_queueServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "queueType.html");
	}

	public void add() {
		setAttr("code", CODE);
//		//所属数据源
		List<Record> list_source=service.findByDb_Source();
		setAttr("list_source", list_source);
//		//所属分诊台
		List<Record> list_triage=service.findByTriageName();
		setAttr("list_triage", list_triage);
		render(BASE_PATH + "queueType_add.html");
	}

	public void edit() {
		String id = getPara(0);
		QueueType queueType = service.findById(id);
		
		setAttr("model", JsonKit.toJson(queueType));
		System.out.println("model"+JsonKit.toJson(queueType));
		setAttr("id", id);
		//setSessionAttr("queuetypeid", id);
		setAttr("code", CODE);
		render(BASE_PATH + "queueType_edit.html");
	}

	public void view() {
		String id = getPara(0);
		
		@SuppressWarnings("unchecked")
		Map<String, Object> map=Blade.dao().selectSingle(VIEW_SOURCE, Record.create().set("queue_type_id", id), Map.class);
		if(null != map && null != map.get("pass_first_flag")){
			map.put("pass_first_flag", Func.getDictName(120, map.get("pass_first_flag")));
		}
		if(null != map && null != map.get("pass_flag_step")){
			map.put("pass_flag_step", Func.getDictName(121, map.get("pass_flag_step")));
		}
		if(null != map && null != map.get("pass_max")){
			map.put("pass_max", Func.getDictName(122, map.get("pass_max")));
		}
		if(null != map && null != map.get("is_checkin")){
			map.put("is_checkin", Func.getDictName(116, map.get("is_checkin")));
		}
		if(null != map && null != map.get("is_pretriage")){
			map.put("is_pretriage", Func.getDictName(117, map.get("is_pretriage")));
		}
		if(null != map && null != map.get("is_reorder")){
			map.put("is_reorder", Func.getDictName(118, map.get("is_reorder")));
		}
		if(null != map && null != map.get("is_reserve")){
			map.put("is_reserve", Func.getDictName(119, map.get("is_reserve")));
		}
		setAttr("model", JsonKit.toJson(map));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "queueType_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new QueueTpyeInterept());
		renderJson(grid);
	}

	public void save() {
		QueueType queueType = mapping(PERFIX, QueueType.class);
		
		QueueType queueType_name=service.findByName(queueType.getName());
		if(null!=queueType_name&&queueType_name.getQueue_type_id()!=null){
			renderJson(error("该队列名称:"+queueType.getName()+"已存在！"));
			return;
		}
		boolean temp = service.save(queueType);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		QueueType queueType = mapping(PERFIX, QueueType.class);
		
		boolean temp = service.update(queueType);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
			String queue_type_id=queueType.getQueue_type_id().toString();
			QueueType qt= service.findById(queue_type_id);
			String type=qt.getIs_checkin().toString();
			boolean bool= qservice.updateisdisplay(Integer.parseInt(queue_type_id),type);
			if(!bool){
				renderJson(error(UPDATE_FAIL_MSG));
				qservice.updateisdisplay(Integer.parseInt(queue_type_id),"1");
			}else{
				renderJson(success(UPDATE_SUCCESS_MSG));
			}
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
	
	
	public void select_queuetype(){
		int type=getParaToInt("type",1);
		int aid=getParaToInt("aid",0);
		
		String sql="select queue_type_id,name,displayname from queue_type ";
		//已选择
		if(type==1){
			sql += " where  triage_id="+aid;
		}else{//未选择
			sql += " where triage_id IS NULL ";
		}
		
		Object grid = paginate(sql);
		
		renderJson(grid);
	}
	
	public void updateChild(){
		
		int type=getParaToInt("type",1);
		int aid=getParaToInt("aid");
		int gid=getParaToInt("gid");
		
		Record record=Record.create().set("aid", aid).set("gid", gid);
		AjaxResult result=new AjaxResult();
		
		result.setCode(0);
		if(type==1){
			
			String sql="UPDATE queue_type set triage_id=#{aid} where queue_type_id =#{gid}";
			Db.init().update(sql,record);
			result.setMessage("添加成功");
		}else{
			String sql="UPDATE queue_type set triage_id=NULL where queue_type_id =#{gid}";
			
			record.remove("aid");
			
			Db.init().update(sql, record);
			result.setMessage("移除成功");
		}
		renderJson(result);
		
	}
	
	/**
	 * 
	* @Title: select_pagerqueuetype 
	* @Description: TODO(叫号器   队列列表) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void select_pagerqueuetype(){
		int type=getParaToInt("type",1);
		int aid=getParaToInt("aid",0);
		
		String sql="";
		//已选择
		if(type==1){
			sql += "SELECT b.id as pager_id, c.queue_type_id, a.onduty, a.is_custom FROM rlt_pager2queue_type a LEFT JOIN pager b ON a.pager_id = b.id LEFT JOIN queue_type c ON a.queue_type_id = c.queue_type_id LEFT JOIN triage d ON c.triage_id = d.triage_id where d.triage_id="+aid;
		}else{//未选择
			sql += "select a.db_source_id,a.source_id,a.queue_type_id,a.name,a.displayname from queue_type a  LEFT JOIN triage b on a.triage_id=b.triage_id WHERE b.triage_id="+aid;
		}
		
		Object grid = paginate(sql);
		
		renderJson(grid);
	}
	
	public void updatePagerChild(){
		
		int type=getParaToInt("type",1);
		int aid=getParaToInt("aid");
		int gid=getParaToInt("gid");
		
		Record record=Record.create().set("aid", aid).set("gid", gid);
		AjaxResult result=new AjaxResult();
		
		result.setCode(0);
		if(type==1){
			
			String sql="INSERT into rlt_pager2queue_type(pager_id,queue_type_id,onduty,is_custom)"
					+ " select b.id,c.queue_type_id,null as onduty,null as is_custom "
					+ "from triage a LEFT JOIN pager b on a.triage_id=b.triage_id LEFT JOIN "
					+ "queue_type c on a.triage_id=c.triage_id WHERE a.triage_id=#{aid} and queue_type_id=#{gid}";
			Db.init().update(sql,record);
			result.setMessage("添加成功");
		}else{
			String sql="delete from rlt_pager2queue_type where pager_id=#{gid}";
			
			record.remove("aid");
			
			Db.init().update(sql, record);
			result.setMessage("移除成功");
		}
		renderJson(result);
		
	}
	/*
	队列类型编号	唯一标识	必要	source_id	Varchar(100)	NOT NULL	排队队列的编码
	队列类型名称		必要	name	Varchar(100)	NOT NULL	排队队列的名称
	队列类型显示名称			displayname	Varchar(255)	NOT NULL	默认与name相同，如有特殊需求，可更改此显示名称默认值
*/
	public void save_queue(){
		Map<String, Object> map=new HashMap<String, Object>();
		String queuetype_strs= getPara("queuetypes");
		if(StrKit.isBlank(queuetype_strs)){
			map.put("code", -1);
			map.put("msg", "请传值");
			renderJson(map);
			return;
		}else{
			JSONArray queuetypeList= JSONArray.parseArray(queuetype_strs);
			if(queuetypeList!=null){
				QueueType queuetype= null;
				for (Object json : queuetypeList) {
					JSONObject queuetype_json = (JSONObject) json;
					queuetype=new QueueType();
					QueueType qt=service.findBySource_id(queuetype_json.getString("source_id"));
					if(null!=qt&&qt.getSource_id()!=null){
						boolean bool= service.updateBy("name='"+queuetype_json.getString("name")+"',displayname='"+queuetype_json.getString("displayname")+"'"," where source_id='"+queuetype_json.getString("source_id")+"'", null);
						if(bool){
							map.put("code", 200);
							map.put("msg", "队列信息修改成功");
							renderJson(map);
						}
						return;
					}else{
						queuetype.setSource_id(queuetype_json.getString("source_id"));
						queuetype.setName(queuetype_json.getString("name"));
						queuetype.setDisplayname(queuetype_json.getString("displayname"));
						queuetype.setIs_checkin(2);
						queuetype.setIs_reorder(2);
						queuetype.setIs_pretriage(2);
						queuetype.setIs_reserve(2);
						boolean bool= service.save(queuetype);
						if (bool) {
							map.put("code", 200);
							map.put("msg", "成功");
							renderJson(map);
						} else {
							map.put("code", -1);
							map.put("msg", "失败");
							renderJson(map);
							return;
						}
					}
				}
			}
		}
		
		/*String source_id=getPara("source_id");
		if(ToolString.isBlank(source_id)){
			map.put("code", -1);
			map.put("msg", "请传入排队队列的编码");
			renderJson(map);
			return;
		}
		String name=getPara("name");
		if(ToolString.isBlank(name)){
			map.put("code", -1);
			map.put("msg", "请传入排队队列的名称");
			renderJson(map);
			return;
		}
		String displayname=getPara("displayname");
		if(ToolString.isBlank(displayname)){
			displayname=name;
		}
		QueueType qt=service.findBySource_id(source_id);
		if(null!=qt&&qt.getSource_id()!=null){
			boolean bool= service.updateBy("name='"+name+"',displayname='"+displayname+"'"," where source_id='"+source_id+"'", null);
			if(bool){
				map.put("code", 200);
				map.put("msg", "队列信息修改成功");
				renderJson(map);
			}
			return;
		}else{
			QueueType q=new QueueType();
			q.setSource_id(source_id);
			q.setName(name);
			q.setDisplayname(displayname);
			q.setIs_checkin(2);
			q.setIs_reorder(2);
			q.setIs_pretriage(2);
			q.setIs_reserve(2);
			boolean bool= service.save(q);
			if(bool){
				map.put("code", 200);
				map.put("msg", "保存成功");
				renderJson(map);
			} else {
				map.put("code", -1);
				map.put("msg", "保存失败");
				renderJson(map);
				return;
			}
		}*/
		renderJson(map);
	}
}
